package tubesrus.servlets;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class Image
 */

public class VideoImport extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public VideoImport() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {

		ServletContext sc = getServletContext();
		String wanted;
		try {
			wanted = new URL(request.getRequestURL().toString()).getFile();

			String filename = "../server/default/data" + wanted;

			String mimeType = sc.getMimeType(filename);

			response.setContentType(mimeType);

			File file = new File(filename);

			response.setContentLength((int) file.length());

			try {
				FileInputStream in;
				in = new FileInputStream(file);

				OutputStream out = response.getOutputStream();

				byte[] buf = new byte[1024];
				int count = 0;
				while ((count = in.read(buf)) >= 0) {
					out.write(buf, 0, count);
				}
				in.close();
				out.close();
			} catch (FileNotFoundException e) {
				System.err.println("Petit problème : non trouvé " + filename);
			} catch (IOException e) {
				System.err.println("Petit problème IOException sur " + filename);
			}
		} catch (MalformedURLException e) {
			System.err.println(request.getRequestURL().toString() + " est mal formée !");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

}
